package com.shiwaixiangcun.customer.module.property

import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.google.gson.reflect.TypeToken
import com.jaeger.recyclerviewdivider.RecyclerViewDivider
import com.shiwaixiangcun.customer.R
import com.shiwaixiangcun.customer.adapter.AdapterMultiPayment
import com.shiwaixiangcun.customer.entity.CommonDataList
import com.shiwaixiangcun.customer.entity.MyPropertyEntity
import com.shiwaixiangcun.customer.utils.JsonUtil
import kotlinx.android.synthetic.main.fragment_multi_payment.*

private const val ARG_PARAM1 = "param1"

/**
 * 多出房产缴费页面
 */
class MultiPaymentFragment : Fragment() {
    private var param1: String? = null
    private var mList: MutableList<MyPropertyEntity>? = null
    private var mAdapter: AdapterMultiPayment? = null
    private var mPaymentDetailFragment: PaymentDetailFragment? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mList = ArrayList()
        mAdapter = AdapterMultiPayment(mList)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
        }
        val type = object : TypeToken<CommonDataList<MyPropertyEntity>>() {}.type
        val fromJson = JsonUtil.fromJson<CommonDataList<MyPropertyEntity>>(param1, type)
        mList?.addAll(fromJson.data)

    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_multi_payment, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView()
    }

    private fun initView() {
        rv_multi_payment.layoutManager = LinearLayoutManager(this.context)
        rv_multi_payment.adapter = mAdapter
        val divider = RecyclerViewDivider.Builder(this.context)
                .setOrientation(RecyclerViewDivider.VERTICAL)
                .setStyle(RecyclerViewDivider.Style.END)
                .setMarginLeft(16f)
                .setMarginRight(16f)
                .setSize(0.5f)
                .setColorRes(R.color.color_divider_0_1)
                .build()
        rv_multi_payment.addItemDecoration(divider)

        mAdapter?.setOnItemClickListener { adapter, _, position ->
            val item = adapter.data[position] as MyPropertyEntity
            mPaymentDetailFragment = PaymentDetailFragment.newInstance(item.id)
            this.activity?.supportFragmentManager
                    ?.beginTransaction()
                    ?.add(R.id.property_content, mPaymentDetailFragment, "detailFragment")
                    ?.addToBackStack(null)
                    ?.commit()

        }

    }


    companion object {
        /**
         * Use this factory method to create a new instance of
         * this fragment using the provided parameters.
         *
         * @param param1 Parameter 1.
         * @param param2 Parameter 2.
         * @return A new instance of fragment MultiPaymentFragment.
         */
        @JvmStatic
        fun newInstance(param1: String) =
                MultiPaymentFragment().apply {
                    arguments = Bundle().apply {
                        putString(ARG_PARAM1, param1)
                    }
                }
    }
}
